<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
冒泡排序 O(n方)
<script>
  function bubble(arr) {
    const len = arr.length
    for (let i = 0; i < len; i++) {
      for (let j = 0; j < len - 1 - i; j++) {
        // 找最大
        if (arr[j] > arr[j + 1]) {
          swap(arr, j, j + 1)
        }
      }
    }
  }

  function swap(arr, a, b) {
    // const temp = arr[a]
    // arr[a] = arr[b]
    // arr[b] = temp

    // [arr[b], arr[a]] = [arr[a], arr[b]]

    arr[a] = arr[a] ^ arr[b]
    arr[b] = arr[a] ^ arr[b]
    arr[a] = arr[a] ^ arr[b]
  }

  const arr = [0, 5, 3, 4, 2, 1]
  bubble(arr)
  console.log(arr)

</script>
</body>
</html>
